using System;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using System.Text;
using UnityEngine;

public class Economy : MonoBehaviour
{
	public class LevelData
	{
		private const char valueSeperator = ';';

		private const char arraySeperator = '|';

		public int id;

		public float record;

		public int currentPoints;

		public int[] attemptsPerPoint;

		public bool visited;

		public LevelData()
		{
			attemptsPerPoint = new int[EconomySettings.timesCountForEachLevel + 1];
		}

		public LevelData(int i, float recordInSeconds, int points)
		{
			id = i;
			record = recordInSeconds;
			currentPoints = points;
			attemptsPerPoint = new int[EconomySettings.timesCountForEachLevel + 1];
		}

		public LevelData(int i, float recordInSeconds, int points, int[] attempts, bool v = false)
		{
			id = i;
			record = recordInSeconds;
			currentPoints = points;
			attemptsPerPoint = attempts;
			visited = v;
		}

		public LevelData(LevelData ld)
		{
			id = ld.id;
			record = ld.record;
			currentPoints = ld.currentPoints;
			attemptsPerPoint = new int[ld.attemptsPerPoint.Length];
			ld.attemptsPerPoint.CopyTo(attemptsPerPoint, 0);
			visited = ld.visited;
		}

		public override string ToString()
		{
			return string.Format("[LevelData] " + (id + 1) + " : " + record + " => " + currentPoints + " visited = " + visited);
		}

		public string Encode()
		{
			StringBuilder stringBuilder = new StringBuilder();
			stringBuilder.Append(id.ToString());
			stringBuilder.Append(';');
			stringBuilder.Append(record.ToString());
			stringBuilder.Append(';');
			stringBuilder.Append(currentPoints.ToString());
			stringBuilder.Append(';');
			if (attemptsPerPoint != null && attemptsPerPoint.Length > 0)
			{
				for (int i = 0; i < attemptsPerPoint.Length - 1; i++)
				{
					stringBuilder.Append(attemptsPerPoint[i]);
					stringBuilder.Append(",");
				}
				stringBuilder.Append(attemptsPerPoint[attemptsPerPoint.Length - 1]);
				stringBuilder.Append(';');
			}
			stringBuilder.Append(visited.ToString());
			return stringBuilder.ToString();
		}

		public void Decode(string encoded)
		{
			if (encoded == null || encoded == string.Empty || encoded == string.Empty)
			{
				return;
			}
			string[] array = encoded.Split(';');
			if (array == null || array.Length == 0)
			{
				return;
			}
			id = int.Parse(array[0]);
			if (array.Length < 2)
			{
				return;
			}
			record = float.Parse(array[1]);
			if (array.Length < 3)
			{
				return;
			}
			currentPoints = int.Parse(array[2]);
			if (array.Length < 4)
			{
				return;
			}
			string[] array2 = array[3].Split(',');
			if (array2 != null && array2.Length == EconomySettings.timesCountForEachLevel + 1)
			{
				attemptsPerPoint = new int[EconomySettings.timesCountForEachLevel + 1];
				for (int i = 0; i < array2.Length; i++)
				{
					attemptsPerPoint[i] = int.Parse(array2[i]);
				}
			}
			if (array.Length >= 5)
			{
				visited = bool.Parse(array[4]);
			}
		}

		public LevelData Copy()
		{
			return new LevelData(this);
		}

		public static string Encode(LevelData[] ldArray)
		{
			if (ldArray == null || ldArray.Length == 0)
			{
				return string.Empty;
			}
			StringBuilder stringBuilder = new StringBuilder();
			for (int i = 0; i < ldArray.Length - 1; i++)
			{
				stringBuilder.Append(ldArray[i].Encode());
				stringBuilder.Append('|');
			}
			stringBuilder.Append(ldArray[ldArray.Length - 1].Encode());
			return stringBuilder.ToString();
		}

		public static bool Decode(string encoded, out LevelData[] ldArray)
		{
			ldArray = null;
			if (encoded == null || encoded == string.Empty || encoded == string.Empty)
			{
				return false;
			}
			string[] array = encoded.Split('|');
			if (array != null && array.Length > 0)
			{
				ldArray = new LevelData[array.Length];
				for (int i = 0; i < array.Length; i++)
				{
					ldArray[i] = new LevelData();
					ldArray[i].Decode(array[i]);
				}
				return true;
			}
			return false;
		}
	}

	public class TutorialLevelData
	{
		private const char valueSeperator = ';';

		private const char arraySeperator = '|';

		public int id;

		public bool started;

		public bool finished;

		public bool startedVideo;

		public bool finishedVideo;

		public TutorialLevelData()
		{
		}

		public TutorialLevelData(int i, bool s, bool f, bool sv, bool fv)
		{
			id = i;
			started = s;
			finished = f;
			startedVideo = sv;
			finishedVideo = fv;
		}

		public TutorialLevelData(TutorialLevelData tld)
		{
			id = tld.id;
			started = tld.started;
			finished = tld.finished;
			startedVideo = tld.startedVideo;
			finishedVideo = tld.finishedVideo;
		}

		public override string ToString()
		{
			return string.Format("[TutorialLevelData] " + (id + 1) + " : " + finished);
		}

		public string Encode()
		{
			StringBuilder stringBuilder = new StringBuilder();
			stringBuilder.Append(id.ToString());
			stringBuilder.Append(';');
			stringBuilder.Append(started.ToString());
			stringBuilder.Append(';');
			stringBuilder.Append(finished.ToString());
			stringBuilder.Append(';');
			stringBuilder.Append(startedVideo.ToString());
			stringBuilder.Append(';');
			stringBuilder.Append(finishedVideo.ToString());
			return stringBuilder.ToString();
		}

		public void Decode(string encoded)
		{
			if (encoded == null || encoded == string.Empty || encoded == string.Empty)
			{
				return;
			}
			string[] array = encoded.Split(';');
			if (array == null || array.Length == 0)
			{
				return;
			}
			id = int.Parse(array[0]);
			if (array.Length < 2)
			{
				return;
			}
			started = bool.Parse(array[1]);
			if (array.Length < 3)
			{
				return;
			}
			finished = bool.Parse(array[2]);
			if (array.Length >= 4)
			{
				startedVideo = bool.Parse(array[3]);
				if (array.Length >= 5)
				{
					finishedVideo = bool.Parse(array[4]);
				}
			}
		}

		public TutorialLevelData Copy()
		{
			return new TutorialLevelData(this);
		}

		public static string Encode(TutorialLevelData[] tldArray)
		{
			if (tldArray == null || tldArray.Length == 0)
			{
				return string.Empty;
			}
			StringBuilder stringBuilder = new StringBuilder();
			for (int i = 0; i < tldArray.Length - 1; i++)
			{
				stringBuilder.Append(tldArray[i].Encode());
				stringBuilder.Append('|');
			}
			stringBuilder.Append(tldArray[tldArray.Length - 1].Encode());
			return stringBuilder.ToString();
		}

		public static bool Decode(string encoded, out TutorialLevelData[] tldArray)
		{
			tldArray = null;
			if (encoded == null || encoded == string.Empty || encoded == string.Empty)
			{
				return false;
			}
			string[] array = encoded.Split('|');
			if (array != null && array.Length > 0)
			{
				tldArray = new TutorialLevelData[array.Length];
				for (int i = 0; i < tldArray.Length; i++)
				{
					tldArray[i] = new TutorialLevelData();
					tldArray[i].Decode(array[i]);
				}
				return true;
			}
			return false;
		}
	}

	public delegate void LivesChangedEventHandler(int deltaLives);

	public delegate void WildcardsChangedEventHandler(int deltaWildcards);

	public delegate void ArenasPurchasedEventHandler();

	public delegate void AdsDisabledEventHandler();

	public delegate void NotificationsEventHandler();

	public delegate void PurchaseFinishedEventHandler(bool success);

	public delegate void RestorePurchasesFinishedEventHandler(bool success);

	public delegate void SharingFinishedEventHandler(bool success);

	public delegate void FinishedWatchingAdEventHandler();

	public delegate void FinishedWatchingRewardedAdEventHandler(bool success);

	private string bonusStartPointsPPKey = "kBonusStartPoints";

	private string livesPPKey = "kLives";

	private string wildcardsPPKey = "kWildcards";

	private string arenasPurchasedPPKey = "kArenasUnlocked";

	private string adsDisabledPPKey = "kAdsDisabled";

	private string dateOfLastAdShownPPKey = "kdateOfLastAdShown";

	private string playTimeInMinutesAtLastRewardedVideoPPKey = "kPlayTimeAtLastRewardedVideo";

	private string totalPlayTimePPKey = "kTotalPlayTime";

	private string livesRewardedPerPlayTimePPKey = "kLivesRewardedPerPlayTime";

	private string livesSpentPerPlayTimePPKey = "kLivesRewardedPerPlayTime";

	private string livesSpentPerWildcardPPKey = "kLivesSpentPerWildcard";

	private string levelDataKeyPrefix = "kLevelData_";

	private string tutorialLevelDataPPKey = "kTutorialLevelData";

	private string totalLivesSpentPPKey = "kTotalLivesSpent";

	private string totalLivesPurchasedPPKey = "kTotalLivesPurchased";

	private string totalLivesRewardedPPKey = "kTotalLivesRewarded";

	private string totalWildcardsSpentPPKey = "kTotalWildcardsSpent";

	private string totalWildcardsPurchasedPPKey = "kTotalWildcardsPurchased";

	private string totalWildcardsRewardedPPKey = "kTotalWildcardsRewarded";

	private string livesPackagesPurchasedPPKey = "kLivesPachakgesPurchased";

	private string wildcardsPackagesPurchasedPPKey = "kWildcardsPachakgesPurchased";

	private string economySettingsPPKeyPrefix = "kEcSettings_";

	private string newEconomySettingsPPKeyPrefix = "kEcSettings_new_";

	private string arenasUnlockedAtLevelFinishPPKey = "kArenasUnlockedAtLevelFinish";

	private string dateOfFirstLaunchPPKey = "kDateOfFirstLaunch";

	private string applicationVersionPPKey = "kAppVersion";

	private string recordAttemptsStartedPPKey = "kRecordAttemptsStarted";

	private string recordAttemptsAchievedPPKey = "kRecordAttemptsAchieved";

	private string hasMadeAPurchasePPKey = "kHasMadeAPurchase";

	public static Economy instance;

	private static int highestFinishedLevel;

	private string _applicationVersion;

	private DateTime dateOfFirstLaunch;

	private bool _applicationVersionChanged;

	private int bonusStartPoints;

	private TutorialLevelData[] userTutorialLevelData;

	private int _lives;

	private int _wildcards;

	private bool _arenasPurchased;

	private bool _adsDisabled;

	private int _totalLivesSpent;

	private int _totalLivesPurchased;

	private int _totalLivesRewarded;

	private int _totalWildcardsSpent;

	private int _totalWildcardsPurchased;

	private int _totalWildcardsRewarded;

	private bool _hasMadeAPurchase;

	private int livesPackagesPurchased;

	private int wildcardsPackagesPurchased;

	private int totalPlayTime;

	private int livesRewardedPerPlayTime;

	private int livesSpentPerPlayTime;

	private int _livesSpentPerWildcard;

	private LevelData[] userLevelData;

	private int _recordAttemptsStarted;

	private int _recordAttemptsAchieved;

	private List<int> arenasUnlockedAtLevelFinish;

	private EconomyVariableSettings currentSettings;

	private EconomyVariableSettings newSettings;

	private DateTime dateOfLastAdShown;

	private DateTime dateAppReceivedFocus;

	private int playTimeInMinutesAtLastRewardedVideo;

	private int livesSpentPerCheckPoint;

	private EconomyTimer bonusLivesTimer;

	private string newLevelTimesURL = "http://crocostudio.com/gyrospheretrialssettingsupdate";

	private bool requestRunning;

	private IAPManager iapManager;

	private SocialManager socialManager;

	private bool rewardOnSuccess;

	private NotificationsManager notificationsManager;

	private AdManager adManager;

	private EconomyPlayTimeTimer playTimeTimer;

	private int lastCheckPoint = -1;

	private int currentCheckPoint;

	private bool levelStarted;

	private int recordAttemptCount;

	private bool recordAttemptInProgress;

	private float recordAttemptRecord = -1f;

	public static bool hasInstance
	{
		get
		{
			return instance != null;
		}
	}

	public static int lives
	{
		get
		{
			if (instance == null)
			{
				return -1;
			}
			return instance._lives;
		}
	}

	public static int wildcards
	{
		get
		{
			if (instance == null)
			{
				return 0;
			}
			return instance._wildcards;
		}
	}

	public static bool gameDataUpdated
	{
		get
		{
			if (instance == null || instance.currentSettings == null || instance.newSettings == null)
			{
				return false;
			}
			int num = instance.currentSettings.date.CompareTo(instance.newSettings.date);
			return instance.newSettings.levelTimes != null && num < 0;
		}
	}

	public static int totalBonusPoints
	{
		get
		{
			if (!gameDataUpdated)
			{
				return 0;
			}
			int[] array = bonusPoints;
			if (array == null)
			{
				return 0;
			}
			int num = 0;
			for (int i = 0; i < array.Length; i++)
			{
				num += array[i];
			}
			return num;
		}
	}

	public static int[] bonusPoints
	{
		get
		{
			if (!gameDataUpdated)
			{
				return null;
			}
			if (instance.userLevelData == null)
			{
				return null;
			}
			int[] array = new int[Globals.levelsCount + 1];
			int num = 0;
			for (int i = 1; i < array.Length; i++)
			{
				num = instance.GetNumberOfPointsForTimeI(i, instance.userLevelData[i - 1].record, instance.newSettings);
				if (num > instance.userLevelData[i - 1].currentPoints)
				{
					array[i] = num - instance.userLevelData[i - 1].currentPoints;
				}
			}
			return array;
		}
	}

	public static bool arenasPurchased
	{
		get
		{
			if (instance == null)
			{
				return false;
			}
			return instance._arenasPurchased;
		}
	}

	public static bool adsDisabled
	{
		get
		{
			if (instance == null)
			{
				return false;
			}
			return instance._adsDisabled;
		}
	}

	public static int totalPoints
	{
		get
		{
			int num = 0;
			num += instance.bonusStartPoints;
			if (instance != null && instance.userLevelData != null && instance.userLevelData.Length != 0)
			{
				for (int i = 0; i < instance.userLevelData.Length; i++)
				{
					num += instance.userLevelData[i].currentPoints;
				}
			}
			return num;
		}
	}

	public static int livesForRecord
	{
		get
		{
			if ((bool)instance)
			{
				return instance.currentSettings.livesForRecord;
			}
			return 0;
		}
	}

	public static bool isFacebookAvailable
	{
		get
		{
			if (instance != null && instance.socialManager != null)
			{
				return instance.socialManager.canUseFB;
			}
			return false;
		}
	}

	public static bool isTwitterAvailable
	{
		get
		{
			if (instance != null && instance.socialManager != null)
			{
				return instance.socialManager.canUseTwitter;
			}
			return false;
		}
	}

	public static bool hasMadeAPurchase
	{
		get
		{
			if (instance != null)
			{
				return instance._hasMadeAPurchase;
			}
			return false;
		}
	}

	public static bool isRewardedAdAvailable
	{
		get
		{
			return false;
		}
	}

	public static bool applicationVersionChanged
	{
		get
		{
			if (instance != null)
			{
				return instance._applicationVersionChanged;
			}
			return false;
		}
	}

	public static int totalLivesSpent
	{
		get
		{
			if (instance == null)
			{
				return 0;
			}
			return instance._totalLivesSpent;
		}
	}

	public static int totalLivesPurchased
	{
		get
		{
			if (instance == null)
			{
				return 0;
			}
			return instance._totalLivesPurchased;
		}
	}

	public static int totalLivesRewarded
	{
		get
		{
			if (instance == null)
			{
				return 0;
			}
			return instance._totalLivesRewarded;
		}
	}

	public static int totalWildcardsSpent
	{
		get
		{
			if (instance == null)
			{
				return 0;
			}
			return instance._totalWildcardsSpent;
		}
	}

	public static int totalWildcardsPurchased
	{
		get
		{
			if (instance == null)
			{
				return 0;
			}
			return instance._totalWildcardsPurchased;
		}
	}

	public static int totalWildcardsRewarded
	{
		get
		{
			if (instance == null)
			{
				return 0;
			}
			return instance._totalWildcardsRewarded;
		}
	}

	public static int livesSpentPerWildcard
	{
		get
		{
			if (instance == null)
			{
				return 0;
			}
			return instance._livesSpentPerWildcard;
		}
	}

	[method: MethodImpl(32)]
	public static event LivesChangedEventHandler LivesChangedEvent;

	[method: MethodImpl(32)]
	public static event WildcardsChangedEventHandler WildcardsChangedEvent;

	[method: MethodImpl(32)]
	public static event ArenasPurchasedEventHandler ArenasPurchasedEvent;

	[method: MethodImpl(32)]
	public static event AdsDisabledEventHandler AdsDisabledEvent;

	[method: MethodImpl(32)]
	public static event NotificationsEventHandler ActiveNotificationsChangedEvent;

	[method: MethodImpl(32)]
	public static event PurchaseFinishedEventHandler PurchaseFinishedEvent;

	[method: MethodImpl(32)]
	public static event RestorePurchasesFinishedEventHandler RestorePurchasesFinishedEvent;

	[method: MethodImpl(32)]
	public static event SharingFinishedEventHandler SharingFinishedEvent;

	[method: MethodImpl(32)]
	public static event FinishedWatchingAdEventHandler FinishedWatchingAdEvent;

	[method: MethodImpl(32)]
	public static event FinishedWatchingRewardedAdEventHandler FinishedWatchingRewardedAdEvent;

	private static void FireLivesChangedEvent(int deltaLives)
	{
		if (Economy.LivesChangedEvent != null)
		{
			Economy.LivesChangedEvent(deltaLives);
		}
	}

	public static int GetBonusPointsForLevel(int levelNumber)
	{
		if (!gameDataUpdated)
		{
			return 0;
		}
		int numberOfPointsForTimeI = instance.GetNumberOfPointsForTimeI(levelNumber, instance.userLevelData[levelNumber - 1].record, instance.newSettings);
		if (numberOfPointsForTimeI > instance.userLevelData[levelNumber - 1].currentPoints)
		{
			return numberOfPointsForTimeI - instance.userLevelData[levelNumber - 1].currentPoints;
		}
		return 0;
	}

	public static void ApplyNewSettings()
	{
		if (instance != null)
		{
			instance.currentSettings.Overwrite(instance.newSettings);
			if (instance.newSettings != null)
			{
				instance.newSettings.DeleteFromPlayerPrefs();
				instance.newSettings = null;
			}
			instance.RecalculatePoints();
			instance.notificationsManager.LoadSettings(instance.currentSettings);
			Debug.Log("Aplied new settings");
		}
	}

	private static bool AddLives(int livesToAdd)
	{
		if (instance != null)
		{
			bool result = true;
			instance._lives += livesToAdd;
			if (instance._lives < 0)
			{
				result = false;
				instance._lives = 0;
			}
			if (instance._lives > EconomySettings.maxLives)
			{
				instance._lives = EconomySettings.maxLives;
			}
			FireLivesChangedEvent(livesToAdd);
			return result;
		}
		return true;
	}

	public static bool ConsumeLife()
	{
		if (instance != null)
		{
			return instance.ConsumeLifeI();
		}
		return true;
	}

	private static void FireWildcardsChangedEvent(int deltaWildcards)
	{
		if (Economy.WildcardsChangedEvent != null)
		{
			Economy.WildcardsChangedEvent(deltaWildcards);
		}
	}

	private static bool AddWildcards(int wildcardsToAdd)
	{
		if (wildcardsToAdd == 0)
		{
			return false;
		}
		if (instance != null)
		{
			bool result = true;
			instance._wildcards += wildcardsToAdd;
			if (instance._wildcards < 0)
			{
				result = false;
				instance._wildcards = 0;
			}
			if (instance._wildcards > EconomySettings.maxWildcards)
			{
				instance._wildcards = EconomySettings.maxWildcards;
			}
			FireWildcardsChangedEvent(wildcardsToAdd);
			return result;
		}
		return true;
	}

	public static bool ConsumeWildcard()
	{
		if (instance != null)
		{
			return instance.ConsumeWildcardI();
		}
		return true;
	}

	private static void FireArenasPurchasedEvent()
	{
		if (Economy.ArenasPurchasedEvent != null)
		{
			Economy.ArenasPurchasedEvent();
		}
	}

	private static void FireAdsDisabledEvent()
	{
		if (Economy.AdsDisabledEvent != null)
		{
			Economy.AdsDisabledEvent();
		}
	}

	public static LevelData GetLevelData(int levelNumber)
	{
		if (instance != null && instance.userLevelData != null && levelNumber >= 1 && levelNumber <= instance.userLevelData.Length)
		{
			return instance.userLevelData[levelNumber - 1];
		}
		return null;
	}

	public static void RefreshHighestFinishedLevel()
	{
		highestFinishedLevel = 0;
		if (instance != null)
		{
			for (int i = 1; i <= Globals.levelsCount; i++)
			{
				LevelData levelData = GetLevelData(i);
				if (levelData.record > 0f)
				{
					highestFinishedLevel = i;
				}
			}
		}
		Debug.Log(Time.frameCount + " RefreshHighestFinishedLevel : " + highestFinishedLevel);
	}

	public static int GetHighestFinishedLevel(bool refresh = false)
	{
		if (refresh)
		{
			RefreshHighestFinishedLevel();
		}
		return highestFinishedLevel;
	}

	public static bool IsNotificationReadyAndAvailable(NotificationsManager.NotificationName notificationName)
	{
		if ((bool)instance)
		{
			return instance.notificationsManager.IsReadyAndAvailable(notificationName);
		}
		return false;
	}

	public static void CompleteNotification(NotificationsManager.NotificationName notificationName)
	{
		if ((bool)instance)
		{
			int notificationReward = GetNotificationReward(notificationName);
			AddLives(notificationReward);
			instance._totalLivesRewarded += notificationReward;
			instance.livesRewardedPerPlayTime += notificationReward;
			instance.notificationsManager.Complete(notificationName);
			instance.SaveInfoToPlayerPrefs();
		}
	}

	public static void ScheduleOSNotificationForDailyReward()
	{
	}

	public static void DismissNotification(NotificationsManager.NotificationName notificationName)
	{
		if ((bool)instance)
		{
			instance.notificationsManager.Dismiss(notificationName);
		}
	}

	public static int GetNotificationRemainingSeconds(NotificationsManager.NotificationName notificationName)
	{
		if ((bool)instance)
		{
			return instance.notificationsManager.GetRemainingSeconds(notificationName);
		}
		return -1;
	}

	public static void FireActiveNotificationsChangedEvent()
	{
		if (Economy.ActiveNotificationsChangedEvent != null)
		{
			Economy.ActiveNotificationsChangedEvent();
		}
	}

	public static bool RefreshActiveNotifications()
	{
		bool flag = false;
		if ((bool)instance)
		{
			flag = instance.notificationsManager.RefreshActiveNotifications();
			if (flag)
			{
				FireActiveNotificationsChangedEvent();
			}
		}
		return flag;
	}

	public static int GetActiveNotificationsCount()
	{
		if ((bool)instance)
		{
			return instance.notificationsManager.GetActiveNotificationsCount();
		}
		return 0;
	}

	public static List<NotificationsManager.NotificationName> GetActiveNotifications()
	{
		if ((bool)instance)
		{
			return instance.notificationsManager.activeNotifications;
		}
		return null;
	}

	public static int GetNotificationReward(NotificationsManager.NotificationName notificationName)
	{
		if ((bool)instance)
		{
			return instance.notificationsManager.GetReward(notificationName);
		}
		return 0;
	}

	public static bool CanPurchase(IAPManager.ProductType pType)
	{
		if (instance != null && instance.iapManager != null)
		{
			switch (pType)
			{
			case IAPManager.ProductType.LivesPack1:
			case IAPManager.ProductType.LivesPack2:
			case IAPManager.ProductType.LivesPack3:
			{
				int quantityForProductId2 = instance.iapManager.GetQuantityForProductId(pType);
				if (lives + quantityForProductId2 > EconomySettings.maxLives)
				{
					return false;
				}
				break;
			}
			case IAPManager.ProductType.WildcardsPack1:
			case IAPManager.ProductType.WildcardsPack2:
			case IAPManager.ProductType.WildcardsPack3:
			{
				int quantityForProductId = instance.iapManager.GetQuantityForProductId(pType);
				if (wildcards + quantityForProductId > EconomySettings.maxWildcards)
				{
					return false;
				}
				break;
			}
			case IAPManager.ProductType.RemoveAds:
				if (instance._adsDisabled)
				{
					return false;
				}
				break;
			case IAPManager.ProductType.Unlock:
				if (instance._arenasPurchased)
				{
					return false;
				}
				break;
			}
			return instance.iapManager.CanPurchase(pType);
		}
		return false;
	}

	public static string GetFormattedPriceForProduct(IAPManager.ProductType pType)
	{
		return IAPManager.GetDefaultFormattedPrice(pType);
	}

	public static int GetQuantityForProduct(IAPManager.ProductType pType)
	{
		if (instance != null && instance.iapManager != null)
		{
			return instance.iapManager.GetQuantityForProductId(pType);
		}
		return -1;
	}

	public static void PurchaseProduct(IAPManager.ProductType productType)
	{
		if (instance != null && CanPurchase(productType))
		{
			instance.PurchaseProductI(productType);
		}
	}

	private static void FirePurchaseFinishedEvent(bool success)
	{
		if (Economy.PurchaseFinishedEvent != null)
		{
			Economy.PurchaseFinishedEvent(success);
		}
	}

	public static void RestorePurchases()
	{
		if (instance != null && instance.iapManager != null)
		{
			instance.iapManager.RestorePurchases();
		}
	}

	private static void FireRestorePurchasesFinishedEvent(bool success)
	{
		if (Economy.RestorePurchasesFinishedEvent != null)
		{
			Economy.RestorePurchasesFinishedEvent(success);
		}
	}

	public static void Share(SocialManager.SharePlatform platform, bool withReward, string imagePath = "")
	{
		if (instance == null)
		{
			return;
		}
		if (withReward)
		{
			switch (platform)
			{
			case SocialManager.SharePlatform.Facebook:
				if (isFacebookAvailable)
				{
					instance.ShareI(SocialManager.SharePlatform.Facebook, withReward, string.Empty);
				}
				break;
			case SocialManager.SharePlatform.Twitter:
				if (isTwitterAvailable)
				{
					instance.ShareI(SocialManager.SharePlatform.Twitter, withReward, string.Empty);
				}
				break;
			default:
				instance.ShareI(platform, withReward, string.Empty);
				break;
			}
		}
		else
		{
			instance.ShareI(platform, withReward, ShareScreenshot.screenshotImagePath);
		}
	}

	private static void FireSharingFinishedEvent(bool succes)
	{
		if (Economy.SharingFinishedEvent != null)
		{
			Economy.SharingFinishedEvent(succes);
		}
	}
	private static void FireFinishedWatchingAdEvent()
	{
		if (Economy.FinishedWatchingAdEvent != null)
		{
			Economy.FinishedWatchingAdEvent();
		}
	}
	private static void FireFinishedWatchingRewardedAdEvent(bool success)
	{
		if (Economy.FinishedWatchingRewardedAdEvent != null)
		{
			Economy.FinishedWatchingRewardedAdEvent(success);
		}
	}

	public static float[] GetPointTimesForLevel(int levelNumber)
	{
		if (instance == null || instance.currentSettings == null)
		{
			if (EconomySettings.timesForLevel != null && EconomySettings.timesForLevel.ContainsKey(levelNumber))
			{
				return EconomySettings.timesForLevel[levelNumber];
			}
			return null;
		}
		return instance.currentSettings.GetTimesForLevel(levelNumber);
	}

	public static int GetNumberOfPointsForTime(int levelNumber, float timeInSeconds)
	{
		if (instance != null && instance.currentSettings != null)
		{
			return instance.GetNumberOfPointsForTimeI(levelNumber, timeInSeconds, instance.currentSettings);
		}
		return -1;
	}

	public static void Save()
	{
		if (instance != null)
		{
			instance.SaveInfoToPlayerPrefs();
		}
	}

	public static int GetWildcardsForArenaUnlock()
	{
		if (instance != null)
		{
			return instance.GetWildcardsForArenaUnlockI();
		}
		return 0;
	}

	public static void AddWildcardsForArenaUnlock()
	{
		if (instance != null)
		{
			instance.AddWildcardsForArenaUnlockI();
		}
	}

	private void Awake()
	{
		if (instance == null)
		{
			instance = this;
		}
		else
		{
			UnityEngine.Object.Destroy(base.gameObject);
		}
	}

	private void Start()
	{
		Init();
	}

	private void OnDestroy()
	{
		SaveInfoToPlayerPrefs();
		instance = null;
	}

	private void OnApplicationPause(bool paused)
	{
		if (instance == null)
		{
			return;
		}
		if (paused)
		{
			SaveInfoToPlayerPrefs();
			PausePlayTimeTimer();
			if (Game.gameState == GameState.Paused && Game.currentLevelNumber != -1 && Globals.appBackgroundReason == AppBackgroundReason.Unknown)
			{
				FromPlayQuitFromApp(Game.currentLevelNumber);
			}
			return;
		}
		Debug.Log("!!!App received FOCUS!!!");
		Init();
		if (notificationsManager.CheckForCheating())
		{
			RefreshActiveNotifications();
			if (!IsNotificationReadyAndAvailable(NotificationsManager.NotificationName.DailyReward))
			{
				ScheduleOSNotificationForDailyReward();
			}
		}
	}

	private void OnApplicationQuit()
	{
		SaveInfoToPlayerPrefs();
	}

	private void Init()
	{
		dateAppReceivedFocus = DateTime.UtcNow;
		LoadInfoFromPlayerPrefs();
		RequestNewSettings();
		InitPlayTimeTimer();
		InitIAP();
		InitSocial();
		InitNotifications();
		RefreshHighestFinishedLevel();
		Game.GameStateChangedEvent -= GameStateChanged;
		Game.GameStateChangedEvent += GameStateChanged;
		Game.CheckpointActivatedEvent -= CheckpointReached;
		Game.CheckpointActivatedEvent += CheckpointReached;
	}

	private bool ConsumeLifeI()
	{
		bool flag = AddLives(-1);
		if (flag)
		{
			_totalLivesSpent++;
			livesSpentPerPlayTime++;
			livesSpentPerCheckPoint++;
			_livesSpentPerWildcard++;
		}
		return flag;
	}

	private bool ConsumeWildcardI()
	{
		bool flag = AddWildcards(-1);
		if (flag)
		{
			_totalWildcardsSpent++;
			CustomAnalytics.LogWildcardUse(Game.currentLevelNumber, lives, _livesSpentPerWildcard);
			_livesSpentPerWildcard = 0;
		}
		return flag;
	}

	private void RequestNewSettings()
	{
		if (!requestRunning)
		{
			StartCoroutine(RequestNewSettingsCoroutine());
		}
	}

	private IEnumerator RequestNewSettingsCoroutine()
	{
		requestRunning = true;
		WWW www = new WWW(newLevelTimesURL);
		yield return www;
		if (www.error != null && www.error != string.Empty)
		{
			Debug.Log("Error getting new settings " + www.error);
			yield break;
		}
		if (www.text == null || www.text == string.Empty || www.text == string.Empty)
		{
			Debug.Log("No text found at adress : " + newLevelTimesURL);
			yield break;
		}
		Debug.Log("Downloaded new settings from : " + newLevelTimesURL);
		bool showJsonLog = false;
		EconomyVariableSettings tempSettings = EconomyVariableSettings.LoadFromJson(newEconomySettingsPPKeyPrefix, www.text, showJsonLog);
		if (tempSettings != null)
		{
			if (newSettings == null)
			{
				newSettings = new EconomyVariableSettings(newEconomySettingsPPKeyPrefix);
			}
			newSettings.Overwrite(tempSettings);
			Debug.Log("Parsed new settings : " + newSettings.date);
		}
		else
		{
			Debug.Log("Could not parse new settings.");
		}
		yield return true;
		www.Dispose();
		requestRunning = false;
	}

	private void LoadInfoFromPlayerPrefs()
	{
		if (_lives != 0)
		{
			return;
		}
		bonusStartPoints = UserPrefs.getInt(bonusStartPointsPPKey);
		if (bonusStartPoints == -1)
		{
			bonusStartPoints = 0;
			LoadUserTutorialLevelData();
			if (userTutorialLevelData != null && userTutorialLevelData.Length != 0)
			{
				for (int i = 0; i < userTutorialLevelData.Length; i++)
				{
					if (userTutorialLevelData[i].finished)
					{
						bonusStartPoints++;
					}
				}
			}
		}
		_lives = UserPrefs.getInt(livesPPKey);
		if (_lives == -1)
		{
			_lives = EconomySettings.startingLives;
		}
		_wildcards = UserPrefs.getInt(wildcardsPPKey);
		if (_wildcards == -1)
		{
			_wildcards = EconomySettings.startingWildcards;
		}
		dateOfFirstLaunch = UserPrefs.getDate(dateOfFirstLaunchPPKey, DateTime.UtcNow.Date);
		_applicationVersion = UserPrefs.getString(applicationVersionPPKey, string.Empty);
		if (_applicationVersion == string.Empty)
		{
			_applicationVersion = Globals.applicationVersion;
		}
		else if (_applicationVersion != Globals.applicationVersion)
		{
			_applicationVersionChanged = true;
			_applicationVersion = Globals.applicationVersion;
		}
		_arenasPurchased = UserPrefs.getBool(arenasPurchasedPPKey);
		_adsDisabled = UserPrefs.getBool(adsDisabledPPKey);
		dateOfLastAdShown = UserPrefs.getDate(dateOfLastAdShownPPKey, DateTime.UtcNow);
		playTimeInMinutesAtLastRewardedVideo = UserPrefs.getInt(playTimeInMinutesAtLastRewardedVideoPPKey, 0);
		_totalLivesSpent = UserPrefs.getInt(totalLivesSpentPPKey, 0);
		_totalLivesPurchased = UserPrefs.getInt(totalLivesPurchasedPPKey, 0);
		_totalLivesRewarded = UserPrefs.getInt(totalLivesRewardedPPKey, 0);
		_totalWildcardsSpent = UserPrefs.getInt(totalWildcardsSpentPPKey, 0);
		_totalWildcardsPurchased = UserPrefs.getInt(totalWildcardsPurchasedPPKey, 0);
		_totalWildcardsRewarded = UserPrefs.getInt(totalWildcardsRewardedPPKey, 0);
		_hasMadeAPurchase = UserPrefs.getBool(hasMadeAPurchasePPKey);
		livesPackagesPurchased = UserPrefs.getInt(livesPackagesPurchasedPPKey, 0);
		wildcardsPackagesPurchased = UserPrefs.getInt(wildcardsPackagesPurchasedPPKey, 0);
		totalPlayTime = UserPrefs.getInt(totalPlayTimePPKey, 0);
		livesSpentPerPlayTime = UserPrefs.getInt(livesSpentPerPlayTimePPKey, 0);
		livesRewardedPerPlayTime = UserPrefs.getInt(livesSpentPerPlayTimePPKey, 0);
		_livesSpentPerWildcard = UserPrefs.getInt(livesSpentPerWildcardPPKey, 0);
		_recordAttemptsStarted = UserPrefs.getInt(recordAttemptsStartedPPKey, 0);
		_recordAttemptsAchieved = UserPrefs.getInt(recordAttemptsAchievedPPKey, 0);
		string @string = UserPrefs.getString(arenasUnlockedAtLevelFinishPPKey, string.Empty);
		if (@string != string.Empty)
		{
			string[] array = @string.Split(',');
			if (array != null && array.Length != 0)
			{
				arenasUnlockedAtLevelFinish = new List<int>();
				string[] array2 = array;
				foreach (string s in array2)
				{
					arenasUnlockedAtLevelFinish.Add(int.Parse(s));
				}
			}
		}
		LoadEconomySettings();
		LoadUserData();
	}

	private void SaveInfoToPlayerPrefs()
	{
		SaveUserData();
		SaveEconomySettings();
		UserPrefs.Save();
	}

	private void LoadUserData()
	{
		LoadUserLevelData_CustomEncoding();
	}

	private void LoadUserLevelData_CustomEncoding()
	{
		string key = levelDataKeyPrefix;
		string @string = UserPrefs.getString(key, string.Empty);
		if (!LevelData.Decode(@string, out userLevelData))
		{
			userLevelData = new LevelData[Globals.levelsCount];
			for (int i = 0; i < userLevelData.Length; i++)
			{
				int[] array = new int[EconomySettings.timesCountForEachLevel + 1];
				for (int j = 0; j < array.Length; j++)
				{
					array[j] = -1;
				}
				userLevelData[i] = new LevelData(i, 0f, 0, array);
			}
		}
		else
		{
			if (userLevelData.Length >= Globals.levelsCount)
			{
				return;
			}
			LevelData[] array2 = new LevelData[Globals.levelsCount];
			for (int k = 0; k < userLevelData.Length; k++)
			{
				array2[k] = userLevelData[k];
			}
			for (int l = userLevelData.Length; l < array2.Length; l++)
			{
				int[] array3 = new int[EconomySettings.timesCountForEachLevel + 1];
				for (int m = 0; m < array3.Length; m++)
				{
					array3[m] = -1;
				}
				array2[l] = new LevelData(l, 0f, 0, array3);
			}
			userLevelData = array2;
		}
	}

	private void LoadUserTutorialLevelData()
	{
		string key = tutorialLevelDataPPKey;
		string @string = UserPrefs.getString(key, string.Empty);
		if (@string != null && @string != string.Empty && @string != string.Empty)
		{
			TutorialLevelData.Decode(@string, out userTutorialLevelData);
		}
	}

	private void SaveUserData()
	{
		UserPrefs.setInt(bonusStartPointsPPKey, bonusStartPoints);
		UserPrefs.setInt(livesPPKey, _lives);
		UserPrefs.setInt(wildcardsPPKey, _wildcards);
		UserPrefs.setBool(arenasPurchasedPPKey, _arenasPurchased);
		UserPrefs.setBool(adsDisabledPPKey, _adsDisabled);
		UserPrefs.setDate(dateOfLastAdShownPPKey, dateOfLastAdShown);
		UserPrefs.setInt(playTimeInMinutesAtLastRewardedVideoPPKey, playTimeInMinutesAtLastRewardedVideo);
		UserPrefs.setDate(dateOfFirstLaunchPPKey, dateOfFirstLaunch);
		UserPrefs.setString(applicationVersionPPKey, _applicationVersion);
		UserPrefs.setInt(totalLivesSpentPPKey, _totalLivesSpent);
		UserPrefs.setInt(totalLivesPurchasedPPKey, _totalLivesPurchased);
		UserPrefs.setInt(totalLivesRewardedPPKey, _totalLivesRewarded);
		UserPrefs.setInt(totalWildcardsSpentPPKey, _totalWildcardsSpent);
		UserPrefs.setInt(totalWildcardsPurchasedPPKey, _totalWildcardsPurchased);
		UserPrefs.setInt(totalWildcardsRewardedPPKey, _totalWildcardsRewarded);
		UserPrefs.setBool(hasMadeAPurchasePPKey, _hasMadeAPurchase);
		UserPrefs.setInt(livesPackagesPurchasedPPKey, livesPackagesPurchased);
		UserPrefs.setInt(wildcardsPackagesPurchasedPPKey, wildcardsPackagesPurchased);
		if (playTimeTimer != null)
		{
			UserPrefs.setInt(totalPlayTimePPKey, playTimeTimer.GetTotalPlayTime());
			UserPrefs.setInt(livesSpentPerPlayTimePPKey, livesSpentPerPlayTime);
			UserPrefs.setInt(livesRewardedPerPlayTimePPKey, livesRewardedPerPlayTime);
		}
		else
		{
			UserPrefs.setInt(totalPlayTimePPKey, totalPlayTime);
			UserPrefs.setInt(livesSpentPerPlayTimePPKey, livesSpentPerPlayTime);
			UserPrefs.setInt(livesRewardedPerPlayTimePPKey, livesRewardedPerPlayTime);
		}
		UserPrefs.setInt(livesSpentPerWildcardPPKey, _livesSpentPerWildcard);
		if (arenasUnlockedAtLevelFinish != null && arenasUnlockedAtLevelFinish.Count > 0)
		{
			string text = string.Empty;
			for (int i = 0; i < arenasUnlockedAtLevelFinish.Count - 1; i++)
			{
				text = text + arenasUnlockedAtLevelFinish[i] + ",";
			}
			text += arenasUnlockedAtLevelFinish[arenasUnlockedAtLevelFinish.Count - 1];
			UserPrefs.setString(arenasUnlockedAtLevelFinishPPKey, text);
		}
		UserPrefs.setInt(recordAttemptsStartedPPKey, _recordAttemptsStarted);
		UserPrefs.setInt(recordAttemptsAchievedPPKey, _recordAttemptsAchieved);
		SaveUserData_CustomEncoding();
	}

	private void SaveUserData_CustomEncoding()
	{
		UserPrefs.setString(levelDataKeyPrefix, LevelData.Encode(userLevelData));
	}

	private void LoadEconomySettings()
	{
		currentSettings = new EconomyVariableSettings(economySettingsPPKeyPrefix);
		currentSettings.LoadFromPlayerPrefs();
		newSettings = new EconomyVariableSettings(newEconomySettingsPPKeyPrefix);
		if (!newSettings.LoadFromPlayerPrefs(false))
		{
			newSettings = null;
		}
	}

	private void SaveEconomySettings()
	{
		if (currentSettings != null)
		{
			currentSettings.SaveToPlayerPrefs();
		}
		if (newSettings != null)
		{
			newSettings.SaveToPlayerPrefs();
		}
	}

	private void RecalculatePoints()
	{
		int num = 0;
		for (int i = 0; i < userLevelData.Length; i++)
		{
			num = GetNumberOfPointsForTimeI(i + 1, userLevelData[i].record, currentSettings);
			if (num > userLevelData[i].currentPoints)
			{
				userLevelData[i].currentPoints = num;
			}
		}
	}

	private void AddWildcardsForArenaUnlockI()
	{
		if (arenasUnlockedAtLevelFinish != null)
		{
			Debug.Log("adding wildcards for unlocking arenas :" + arenasUnlockedAtLevelFinish.Count);
			_totalWildcardsRewarded += EconomySettings.wildcardsForArenaUnlock * arenasUnlockedAtLevelFinish.Count;
			AddWildcards(EconomySettings.wildcardsForArenaUnlock * arenasUnlockedAtLevelFinish.Count);
			arenasUnlockedAtLevelFinish.Clear();
			arenasUnlockedAtLevelFinish = null;
		}
	}

	private int GetWildcardsForArenaUnlockI()
	{
		int num = 0;
		if (arenasUnlockedAtLevelFinish != null)
		{
			num += EconomySettings.wildcardsForArenaUnlock * arenasUnlockedAtLevelFinish.Count;
		}
		return num;
	}

	public void Reset()
	{
	}

	private void InitIAP()
	{
		if (iapManager == null)
		{
			iapManager = new IAPManager();
			iapManager.PurchaseSuccessEvent += IAPSuccess;
			iapManager.PurchaseFailedEvent += IAPFailed;
			iapManager.RestorePurchaseFinishedEvent += RestoreFinished;
			iapManager.ProductListReceivedEvent += ProductListReceived;
		}
	}

	private void PurchaseProductI(IAPManager.ProductType productType)
	{
		if (iapManager != null)
		{
			iapManager.Purchase(productType);
		}
	}

	public void IAPSuccess(IAPManager.ProductType productType)
	{
		int num = 50;
		int num2 = 5;
		switch (productType)
		{
		case IAPManager.ProductType.LivesPack1:
		case IAPManager.ProductType.LivesPack2:
		case IAPManager.ProductType.LivesPack3:
			//num = iapManager.GetQuantityForProductId(productType);
			AddLives(num);
			_totalLivesPurchased += num;
			livesPackagesPurchased++;
			break;
		case IAPManager.ProductType.WildcardsPack1:
		case IAPManager.ProductType.WildcardsPack2:
		case IAPManager.ProductType.WildcardsPack3:
			//num2 = iapManager.GetQuantityForProductId(productType);
			AddWildcards(num2);
			_totalWildcardsPurchased += num2;
			wildcardsPackagesPurchased++;
			break;
		case IAPManager.ProductType.RemoveAds:
			if (!_adsDisabled)
			{
				_adsDisabled = true;
				AddLives(EconomySettings.livesBonusForRemoveAdPurchase);
				_totalLivesPurchased += EconomySettings.livesBonusForRemoveAdPurchase;
				AddWildcards(EconomySettings.wildcardsBonusForRemoveAdPurchase);
				_totalWildcardsPurchased += EconomySettings.wildcardsBonusForRemoveAdPurchase;
				FireAdsDisabledEvent();
			}
			break;
		case IAPManager.ProductType.Unlock:
			
				_arenasPurchased = true;
				if (IAPManager.DoesPurchaseRemoveAds(productType))
				{
					_adsDisabled = true;
				}
				AddLives(EconomySettings.livesBonusForUnlockPurchase);
				_totalLivesPurchased += EconomySettings.livesBonusForUnlockPurchase;
				AddWildcards(EconomySettings.wildcardsBonusForUnlockPurchase);
				_totalWildcardsPurchased += EconomySettings.wildcardsBonusForUnlockPurchase;
				FireArenasPurchasedEvent();
			
			break;
		}
		_hasMadeAPurchase = true;
		CustomAnalytics.LogIAPByPlayTime(productType, playTimeTimer.GetLastTimeTrigger());
		SaveInfoToPlayerPrefs();
		FirePurchaseFinishedEvent(true);
	}

	private void IAPFailed()
	{
		FirePurchaseFinishedEvent(false);
	}

	private void RestoreFinished(bool success)
	{
		SaveInfoToPlayerPrefs();
		FireRestorePurchasesFinishedEvent(success);
	}

	private void ProductListReceived()
	{
	}

	private void InitSocial()
	{
		if (socialManager == null)
		{
			socialManager = new SocialManager();
			socialManager.ShareResultEvent += SharingResult;
		}
	}

	private void ShareI(SocialManager.SharePlatform platform, bool withReward, string imagePath = "")
	{
		if (socialManager != null)
		{
			rewardOnSuccess = withReward;
			socialManager.Share(platform, withReward, imagePath);
		}
	}

	private void SharingResult(SocialManager.SharePlatform platform, bool success)
	{
		Debug.Log(string.Concat(platform, " Sharing result ", success, " with reward = ", rewardOnSuccess));
		rewardOnSuccess = false;
		FireSharingFinishedEvent(success);
	}

	private void InitNotifications()
	{
		if (notificationsManager == null)
		{
			notificationsManager = new NotificationsManager();
			notificationsManager.LoadSettings(currentSettings);
			notificationsManager.CheckForCheating();
			RefreshActiveNotifications();
		}
	}

	public static AdManager GetAdManager()
	{
		if (instance != null)
		{
			return instance.adManager;
		}
		return null;
	}
	private void FinishedWatchingRewardedVideo(bool success)
	{
		FireFinishedWatchingRewardedAdEvent(success);
	}

	private void FinishedWatchingAd()
	{
		dateOfLastAdShown = DateTime.UtcNow;
		FireFinishedWatchingAdEvent();
	}

	public static int GetTotalPlayTime()
	{
		if (instance != null)
		{
			if (instance.playTimeTimer != null)
			{
				return instance.playTimeTimer.GetTotalPlayTime();
			}
			Debug.Log("PlayTimeTimer NOT FOUND !!!");
		}
		return 0;
	}

	private void InitPlayTimeTimer()
	{
		Debug.Log("init play timer");
		if (playTimeTimer == null)
		{
			playTimeTimer = base.gameObject.AddComponent<EconomyPlayTimeTimer>();
			playTimeTimer.Init(totalPlayTime);
			playTimeTimer.PlayTimeTriggerEvent += PlayTimeChanged;
			playTimeTimer.StartTimer();
		}
	}

	private void StartPlayTimeTimer()
	{
		if (playTimeTimer != null)
		{
			playTimeTimer.StartTimer();
		}
	}

	private void PausePlayTimeTimer()
	{
		if (playTimeTimer != null)
		{
			playTimeTimer.PauseTimer();
		}
	}

	private void PlayTimeChanged(string newPlayTimeString)
	{
		int daysSinceFirstRun = Mathf.RoundToInt((float)DateTime.UtcNow.Date.Subtract(dateOfFirstLaunch).TotalDays);
		CustomAnalytics.LogPlayTimeTrigger(newPlayTimeString, livesRewardedPerPlayTime, livesSpentPerPlayTime, totalLivesRewarded, totalLivesSpent, livesPackagesPurchased, wildcardsPackagesPurchased, _adsDisabled, _arenasPurchased, totalPoints, daysSinceFirstRun);
		livesSpentPerPlayTime = 0;
		livesRewardedPerPlayTime = 0;
	}

	private void CheckpointReached(int checkPointNumber)
	{
		if (checkPointNumber == -1)
		{
			Debug.Log("WTF!!! REACHED CHECKPOINT -1:  lastCheckPoint=" + lastCheckPoint + " currentCheckPoint=" + currentCheckPoint);
		}
		if ((Game.gameState == GameState.Playing || Game.gameState == GameState.Finished) && currentCheckPoint != checkPointNumber)
		{
			lastCheckPoint = currentCheckPoint;
			currentCheckPoint = checkPointNumber;
			if (Game.currentGameMode != GameMode.NormalWildcard)
			{
				CustomAnalytics.LogLivesSpentBetweenCheckpoints(Game.currentLevelNumber, lastCheckPoint, currentCheckPoint, livesSpentPerCheckPoint);
			}
			livesSpentPerCheckPoint = 0;
		}
	}

	private void GameStateChanged()
	{
		GameState gameStatePrevious = Game.gameStatePrevious;
		GameState gameState = Game.gameState;
		if (Game.currentGameMode == GameMode.Normal && gameState == GameState.Playing && (gameStatePrevious == GameState.ReadyToContinue || gameStatePrevious == GameState.ReadyToStart))
		{
			ConsumeLife();
		}
		if (gameStatePrevious == GameState.SpawningForRestart && gameState == GameState.ReadyToStart)
		{
			GeneralLevelRestarted();
		}
		if (gameState == GameState.LevelLoading)
		{
			GeneralLevelInit();
			return;
		}
		int currentLevelNumber = Game.currentLevelNumber;
		int num = currentLevelNumber - 1;
		if (currentLevelNumber == -1)
		{
			Log("ERROR : invalid level number!");
		}
		else if ((Game.currentGameMode == GameMode.Normal || Game.currentGameMode == GameMode.NormalWildcard) && (num < 0 || num >= userLevelData.Length))
		{
			Log("level id not found in levels array");
		}
		else if (gameState == GameState.ReadyToStart)
		{
			GeneralLevelEntered(currentLevelNumber);
		}
		else if (gameStatePrevious == GameState.ReadyToStart && gameState == GameState.Playing)
		{
			GeneralLevelStarted(currentLevelNumber);
		}
		else if (gameStatePrevious == GameState.Playing && gameState == GameState.Finished)
		{
			GeneralLevelFinished(currentLevelNumber);
		}
		else if (((gameStatePrevious == GameState.Paused && gameState == GameState.NoGame) || (gameStatePrevious == GameState.ReadyToContinue && gameState == GameState.NoGame)) && (Game.currentGameMode == GameMode.Normal || Game.currentGameMode == GameMode.NormalWildcard))
		{
			FromPlayQuitToMenu(currentLevelNumber);
		}
	}

	private void GeneralLevelInit()
	{
		ResetRecordAttemptData();
	}

	private void GeneralLevelEntered(int levelNumber)
	{
		GameMode currentGameMode = Game.currentGameMode;
		if (currentGameMode == GameMode.Normal || currentGameMode == GameMode.NormalWildcard)
		{
			LevelEntered(levelNumber);
		}
	}

	private void GeneralLevelStarted(int levelNumber)
	{
		GameMode currentGameMode = Game.currentGameMode;
		if (currentGameMode == GameMode.Normal || currentGameMode == GameMode.NormalWildcard)
		{
			LevelStarted(levelNumber);
		}
	}

	private void GeneralLevelFinished(int levelNumber)
	{
		GameMode currentGameMode = Game.currentGameMode;
		if (currentGameMode == GameMode.Normal || currentGameMode == GameMode.NormalWildcard)
		{
			LevelFinished(levelNumber);
		}
	}

	private void GeneralLevelRestarted()
	{
		GameMode currentGameMode = Game.currentGameMode;
		if (currentGameMode == GameMode.Normal || currentGameMode == GameMode.NormalWildcard)
		{
			LevelRestarted();
		}
	}

	private void LevelEntered(int levelNumber)
	{
		LogFirstLevelEntry(levelNumber);
	}

	private void LevelStarted(int levelNumber)
	{
		lastCheckPoint = -1;
		currentCheckPoint = 0;
		IncreaseAttempts(levelNumber);
		CheckRecordAttemptStart(levelNumber);
		if (Game.currentGameMode != GameMode.NormalWildcard)
		{
			CustomAnalytics.LogLivesSpentBetweenCheckpoints(levelNumber, 0, 0, livesSpentPerCheckPoint);
		}
		levelStarted = true;
	}

	private void LevelFinished(int levelNumber)
	{
		int num = levelNumber - 1;
		float levelTime = Game.levelTime;
		CustomAnalytics.LogFinishTime(levelNumber, levelTime);
		CheckpointReached(99);
		CheckRecordAttemptFinish(levelNumber);
		livesSpentPerCheckPoint = 0;
		int numberOfPointsForTimeI = GetNumberOfPointsForTimeI(levelNumber, levelTime, currentSettings);
		if (numberOfPointsForTimeI == -1)
		{
			Debug.Log("Could not get points for level " + levelNumber);
			return;
		}
		int num2 = totalPoints;
		if (userLevelData[num].record == 0f)
		{
			userLevelData[num].record = levelTime;
			userLevelData[num].currentPoints = numberOfPointsForTimeI;
			int num3 = userLevelData[num].attemptsPerPoint[0];
			userLevelData[num].attemptsPerPoint[0] = -1;
			for (int i = 0; i < numberOfPointsForTimeI; i++)
			{
				if (userLevelData[num].attemptsPerPoint[i] == -1)
				{
					CustomAnalytics.LogPointsEarnedByAttempts(levelNumber, i, 0);
					userLevelData[num].attemptsPerPoint[i] = 0;
				}
			}
			CustomAnalytics.LogPointsEarnedByAttempts(levelNumber, numberOfPointsForTimeI, num3);
			userLevelData[num].attemptsPerPoint[numberOfPointsForTimeI] = num3;
		}
		else if (levelTime < userLevelData[num].record)
		{
			if (userLevelData[num].currentPoints == EconomySettings.maxPointsForLevel)
			{
				_totalLivesRewarded += currentSettings.livesForRecord;
				livesRewardedPerPlayTime += currentSettings.livesForRecord;
				AddLives(currentSettings.livesForRecord);
			}
			userLevelData[num].record = levelTime;
			if (numberOfPointsForTimeI > userLevelData[num].currentPoints)
			{
				int num4 = -1;
				for (int num5 = userLevelData[num].attemptsPerPoint.Length - 1; num5 >= 0; num5--)
				{
					if (userLevelData[num].attemptsPerPoint[num5] != -1)
					{
						num4 = userLevelData[num].attemptsPerPoint[num5];
						userLevelData[num].attemptsPerPoint[num5] = -1;
						break;
					}
				}
				if (num4 != -1)
				{
					for (int j = 0; j < numberOfPointsForTimeI; j++)
					{
						if (userLevelData[num].attemptsPerPoint[j] == -1)
						{
							CustomAnalytics.LogPointsEarnedByAttempts(levelNumber, j, 0);
							userLevelData[num].attemptsPerPoint[j] = 0;
						}
					}
					CustomAnalytics.LogPointsEarnedByAttempts(levelNumber, numberOfPointsForTimeI, num4);
					userLevelData[num].attemptsPerPoint[numberOfPointsForTimeI] = num4;
				}
				userLevelData[num].currentPoints = numberOfPointsForTimeI;
			}
		}
		if (!arenasPurchased)
		{
			arenasUnlockedAtLevelFinish = new List<int>();
			int num6 = totalPoints;
			for (int k = 1; k <= Globals.arenaCount; k++)
			{
				int unlockPointsForArena = EconomySettings.GetUnlockPointsForArena(k);
				if (num2 < unlockPointsForArena && num6 >= unlockPointsForArena)
				{
					Debug.Log("need to add wildcards for arena " + k);
					arenasUnlockedAtLevelFinish.Add(k);
				}
			}
		}
		levelStarted = false;
	}

	private void LevelRestarted()
	{
		livesSpentPerCheckPoint = 0;
	}

	private void FromPlayQuitToMenu(int levelNumber)
	{
		if (levelStarted && Game.currentGameMode != GameMode.NormalWildcard)
		{
			CustomAnalytics.LogLivesSpentCheckpointToExit(levelNumber, currentCheckPoint, livesSpentPerCheckPoint);
		}
		livesSpentPerCheckPoint = 0;
		levelStarted = false;
	}

	private void FromPlayQuitFromApp(int levelNumber)
	{
		if (levelStarted && Game.currentGameMode != GameMode.NormalWildcard)
		{
			CustomAnalytics.LogLivesSpentCheckpointToExit(levelNumber, currentCheckPoint, livesSpentPerCheckPoint);
		}
		livesSpentPerCheckPoint = 0;
	}

	private void LogFirstLevelEntry(int levelNumber)
	{
		int num = levelNumber - 1;
		if (!userLevelData[num].visited)
		{
			CustomAnalytics.LogFirstLevelPlay(num + 1);
			userLevelData[num].visited = true;
		}
	}

	private void IncreaseAttempts(int levelNumber)
	{
		int num = levelNumber - 1;
		int currentPoints = userLevelData[num].currentPoints;
		if (currentPoints == EconomySettings.maxPointsForLevel)
		{
			return;
		}
		if (userLevelData[num].record == 0f)
		{
			if (userLevelData[num].attemptsPerPoint[0] == -1)
			{
				userLevelData[num].attemptsPerPoint[0] = 1;
			}
			else
			{
				userLevelData[num].attemptsPerPoint[0]++;
			}
			Debug.Log("increasing attempts , first time playing , points 0 to " + userLevelData[num].attemptsPerPoint[0]);
			return;
		}
		if (userLevelData[num].attemptsPerPoint[currentPoints + 1] == -1)
		{
			userLevelData[num].attemptsPerPoint[currentPoints + 1] = 1;
		}
		else
		{
			userLevelData[num].attemptsPerPoint[currentPoints + 1]++;
		}
		Debug.Log("increasing attempts for points " + (currentPoints + 1) + " to " + userLevelData[num].attemptsPerPoint[currentPoints + 1]);
	}

	private void ResetRecordAttemptData()
	{
		recordAttemptRecord = -1f;
		recordAttemptCount = 0;
		recordAttemptInProgress = false;
	}

	private void CheckRecordAttemptStart(int levelNumber)
	{
		int num = levelNumber - 1;
		int currentPoints = userLevelData[num].currentPoints;
		if (currentPoints >= EconomySettings.maxPointsForLevel)
		{
			if (!recordAttemptInProgress)
			{
				recordAttemptInProgress = true;
				recordAttemptRecord = userLevelData[num].record;
				_recordAttemptsStarted++;
				CustomAnalytics.LogRecordAttemptStarted(levelNumber, lives, _recordAttemptsStarted);
			}
			recordAttemptCount++;
		}
	}

	private void CheckRecordAttemptFinish(int levelNumber)
	{
		if (recordAttemptInProgress && Game.levelTime < recordAttemptRecord)
		{
			_recordAttemptsAchieved++;
			CustomAnalytics.LogRecordAttemptAchieved(levelNumber, recordAttemptCount, _recordAttemptsAchieved);
			ResetRecordAttemptData();
		}
	}

	private void Log(string message)
	{
	}

	private int GetNumberOfPointsForTimeI(int levelNumber, float timeInSeconds, EconomyVariableSettings settings)
	{
		if (settings == null || timeInSeconds == 0f)
		{
			return -1;
		}
		float[] timesForLevel = settings.GetTimesForLevel(levelNumber);
		if (timesForLevel == null || timesForLevel.Length == 0)
		{
			return -1;
		}
		int result = -1;
		if (timeInSeconds > timesForLevel[0])
		{
			result = 0;
		}
		else if (timeInSeconds <= timesForLevel[timesForLevel.Length - 1])
		{
			result = timesForLevel.Length;
		}
		else
		{
			for (int i = 1; i < timesForLevel.Length; i++)
			{
				if (timeInSeconds <= timesForLevel[i - 1] && timeInSeconds > timesForLevel[i])
				{
					result = i;
					break;
				}
			}
		}
		return result;
	}
}
